package com.wh.wisdomsite.xbox.zywl.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.pojo.zywl.GetPushDataOut;
import com.wh.wisdomsite.xbox.utils.Const;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.server.connToserver_HttpClientClose;
import com.wh.wisdomsite.xbox.zywl.dao.ZywlUserMapper;
import com.wh.wisdomsite.xbox.zywl.entity.ZywlUser;
import com.wh.wisdomsite.xbox.zywl.entity.ZywlUserTicket;
import com.wh.wisdomsite.xbox.zywl.service.IZywlUserService;
import com.wh.wisdomsite.xbox.zywl.service.IZywlUserTicketService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 票务用户ServiceImpl
 * @author 谢泽鹏
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.vi.dao
 * @date 2018年09月05日 15:53
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Service("zywlUserService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class ZywlUserServiceImpl extends ServiceImpl<ZywlUserMapper,ZywlUser> implements IZywlUserService {

      private static final Log logger = LogFactory.getLog(ZywlUserServiceImpl.class);


      @Autowired
      private ZywlUserMapper zywlUserMapper;

      @Autowired
      private IZywlUserTicketService zywlUserTicketService;


      /**
       * 保存或更新
       * @param zywlUser
       * @return
       */
      @Override
      public String saveOrUpdate(ZywlUser zywlUser) throws Exception {
            String uid = "";
            try {
                  String idcardId = zywlUser.getIdcardId();
                  logger.info("saveOrUpdate->>ZywlUser->>身份证号： " + idcardId);
                  if(StringUtils.isNotEmpty(idcardId)){
                        if(idcardIdIsExits(idcardId)){
                              //已存在[身份证], 更新
                              EntityWrapper<ZywlUser> entityWrapper = new EntityWrapper<>();
                              entityWrapper.eq("idcardId", idcardId);
                              zywlUserMapper.update(zywlUser, entityWrapper);
                              zywlUser = zywlUserMapper.selectOne(zywlUser);
                              uid = zywlUser.getId();
                              logger.info("saveOrUpdate->>ZywlUser->>已存在[身份证号]->>执行更新");
                        }else{
                              //不存在[身份证], 添加
                              zywlUser.setCreateData(new Date());
                              zywlUserMapper.insert(zywlUser);
                              uid = zywlUser.getId();
                              logger.info("saveOrUpdate->>ZywlUser->>不存在[身份证号]->>执行添加");
                        }
                  }
            } catch (Exception e){
                  e.printStackTrace();
                  logger.info("saveOrUpdate->>ZywlUser->>Exception： " + e.getMessage());
                  throw new Exception("saveOrUpdate->>ZywlUser->>Exception = " + e.getMessage());
            }
                  return uid;
      }


      /**
       * 判断身份证号是否存在
       * @param idcardId
       * @return
       */
      @Override
      @Transactional(propagation=Propagation.NESTED)
      public boolean idcardIdIsExits(String idcardId) {
            ZywlUser model = new ZywlUser();
            model.setIdcardId(idcardId);
            ZywlUser zywlUser = zywlUserMapper.selectOne(model);
            return null != zywlUser ? true : false;
      }


      /**
       * 同步用户和票务信息
       * @param getPushDataOut
       */
      @Override
      public void synGetPushDataOut(GetPushDataOut getPushDataOut) throws Exception {
            try {
                  logger.info("synGetPushDataOut->>开始");
                  String uid = "";
                  String idcardId = getPushDataOut.getZywlUser().getIdcardId();
                  String userTicketId = "";
                  //同步用户
                  if(null != getPushDataOut.getZywlUser()){
                        //身份证号不为空, 开始同步
                        logger.info("synGetPushDataOut->>身份证号: " + idcardId);
                        if(StringUtils.isNotEmpty(idcardId)){
                              logger.info("synGetPushDataOut->>身份证号->>执行同步");
                              uid = saveOrUpdate(getPushDataOut.getZywlUser());
                        }
                  }
                  //同步票务
                  if(null != getPushDataOut.getUserTicketList()){
                        logger.info("synGetPushDataOut->>用户编号: " + uid);
                        //回调参数
                        Map<String, String> paramMap = new HashMap<>();
                        List<Map> list = new ArrayList<>();
                        for(ZywlUserTicket zywlUserTicket : getPushDataOut.getUserTicketList()){
                              userTicketId = zywlUserTicket.getUserTicketId();
                              logger.info("synGetPushDataOut->>票务编号: " + userTicketId);
                              zywlUserTicket.setUid(uid);
                              //票号不为空, 开始同步
                              if(StringUtils.isNotEmpty(userTicketId)){
                                    logger.info("synGetPushDataOut->>票务编号->>执行同步");
                                    zywlUserTicketService.saveOrUpdate(zywlUserTicket);
                                    Map<String, Object> userTicketMap = new HashMap<>();
                                    userTicketMap.put("userTicketId", userTicketId);
                                    list.add(userTicketMap);
                              }
                        }
                        paramMap.put("userTicketIds", new JSONObject().put("list", list).toString());
                        //调用票务平台的出票接口
                        String json = connToserver_HttpClientClose.sendPostRequest(Const.ZYWL_UPDATEOUTOFUSERTICKET, paramMap);

                  }
                  logger.info("synGetPushDataOut->>结束");
            } catch (Exception e){
                  e.printStackTrace();
                  logger.info("synGetPushDataOut->>Exception： " + e.getMessage());
                  throw new Exception("synGetPushDataOut->>Exception = " + e.getMessage());
            }
      }



}

